package com.example.function.sys.model.pojo;

/**
 * <h1>用户最小属性带项目角色</h1>
 * Created by hanqf on 2021/8/3 10:35.
 */


import com.example.common.support.ApplicationContextProvider;
import com.example.function.project.model.one.ProjectRole;
import com.example.function.project.model.one.ProjectUserRole;
import com.example.function.project.service.IProjectRoleService;
import com.example.function.sys.model.one.SysUser;
import com.example.function.sys.service.ISysUserService;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户查询返回结果封装,带项目角色
 */
@Data
public class SysUserMiniAndProjectRolesPojo implements Serializable {
    private static final long serialVersionUID = 4867853635645130278L;

    /*
     * 主键id
     */
    private Long id;


    /*
     * 登录帐号，供应商邮箱
     */
    private String userLoginName;

    /*
     * 姓名，供应商全称
     */
    private String userName;
    /**
     * 项目角色
     */
    private ProjectRole projectRole;

    public static SysUserMiniAndProjectRolesPojo wrapper(ProjectUserRole projectUserRole) {
        SysUserMiniAndProjectRolesPojo sysUserMiniAndProjectRolesPojo = new SysUserMiniAndProjectRolesPojo();

        if (projectUserRole != null) {
            BeanUtils.copyProperties(projectUserRole, sysUserMiniAndProjectRolesPojo);

            final ISysUserService sysUserService = (ISysUserService) ApplicationContextProvider.getBean("sysUserService");
            final SysUser sysUser = sysUserService.findById(projectUserRole.getUserId());
            sysUserMiniAndProjectRolesPojo.setId(sysUser.getId());
            sysUserMiniAndProjectRolesPojo.setUserName(sysUser.getUserName());
            sysUserMiniAndProjectRolesPojo.setUserLoginName(sysUser.getUserLoginName());

            final IProjectRoleService projectRoleService = (IProjectRoleService) ApplicationContextProvider.getBean("projectRoleService");
            ProjectRole role = projectRoleService.findById(projectUserRole.getProjectRoleId());
            sysUserMiniAndProjectRolesPojo.setProjectRole(role);

        }


        return sysUserMiniAndProjectRolesPojo;
    }

    public static List<SysUserMiniAndProjectRolesPojo> wrapper(List<ProjectUserRole> projectUserRoles) {
        List<SysUserMiniAndProjectRolesPojo> list = new ArrayList<>();
        for (ProjectUserRole projectUserRole : projectUserRoles) {
            list.add(SysUserMiniAndProjectRolesPojo.wrapper(projectUserRole));
        }
        return list;
    }
}

